//
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.rjxy.demo.controller;

import com.rjxy.demo.common.Result;
import com.rjxy.demo.pojo.HeroInfo;
import com.rjxy.demo.pojo.SkillInfo;
import com.rjxy.demo.server.SkillInfoServer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/skill")
public class SkillController {
    @Autowired
    private SkillInfoServer skillInfoServer;
    private static final Logger log = Logger.getLogger(SkillController.class);

    @GetMapping
    public Result<List<SkillInfo>> getAll() {
        try {
            List<SkillInfo> list = skillInfoServer.getAll();
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询技能列表失败", e);
            return Result.error("查询失败");
        }
    }

    @PostMapping
    public Result<Void> addSkill(@RequestBody SkillInfo skillInfo) {
        try {
            skillInfoServer.saveSkill(skillInfo);
            return Result.success(null);
        } catch (Exception e) {
            log.error("新增技能失败", e);
            return Result.error("新增失败");
        }
    }

    @GetMapping("/{sid}")
    public Result<SkillInfo> getById(@PathVariable Integer sid) {
        try {
            log.info("开始查询技能信息，sid=" + sid);
            SkillInfo skill = skillInfoServer.getBySid(sid);
            log.info("查询结果：" + skill);
            if (skill == null) {
                return Result.error("技能不存在");
            }
            if (skill.getHeroInfo() == null) {
                HeroInfo heroInfo = new HeroInfo();
                heroInfo.setHname("无");
                skill.setHeroInfo(heroInfo);
            }
            return Result.success(skill);
        } catch (Exception e) {
            log.error("查询技能详情失败，sid=" + sid, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @PutMapping
    public Result<Void> updateSkill(@RequestBody SkillInfo skillInfo) {
        try {
            skillInfoServer.updateSkill(skillInfo);
            return Result.success(null);
        } catch (Exception e) {
            log.error("修改技能失败", e);
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{sid}")
    public Result<Void> deleteSkill(@PathVariable Integer sid) {
        try {
            skillInfoServer.removeSkill(sid);
            return Result.success(null);
        } catch (Exception e) {
            log.error("删除技能失败", e);
            return Result.error("删除失败");
        }
    }
}
